
window.AboutMe = {_name:"AboutMe", error:function (msg, options) {
	options = options || {};
	options.title = options.title || "It's not you, it's us";
	var dd = AboutMe.alert(msg, options);
	dd.parents(".ui-dialog").addClass("ui-dialog-error");
	return dd;
}, alert:function (msg, options) {
	if (!msg) {
		return;
	}
	options = options || {};
	var dd = $("<div><p class=\"alert-message\">" + msg + "</p></div>");
	options.modal = true;
	options.bgiframe = true, options.draggable = false;
	options.resizable = false;
	options.closeOnEscape = true;
	options.close = options.close || function () {
		$(this).dialog("destroy");
		dd.remove();
	};
	options.buttons = options.buttons || {"OK":function () {
		$(this).dialog("close");
	}};
	options.title = options.title || "";
	dd.dialog(options);
	return dd;
}, ajax:{_name:"AboutMe.ajax", _error:function (api_call, options, data, textStatus, errorThrown, error_hook) {
	if (!error_hook) {
		AboutMe.error("AJAX api call error: " + api_call + ": " + textStatus + ": " + JSON.stringify(data));
	} else {
		error_hook(data, options, api_call, textStatus, errorThrown);
	}
}, _begin:function (api_call, options) {
	if (!options.begin) {
	} else {
		options.begin(api_call, options);
	}
}, _complete:function (api_call, options, textStatus, complete_hook) {
	if (!complete_hook) {
	} else {
		complete_hook(options, api_call, textStatus);
	}
}, _success:function (api_call, options, data, textStatus, success_hook) {
	if (!success_hook) {
		AboutMe.alert("AJAX callback successful: " + api_call + ": " + textStatus + ": " + JSON.stringify(data));
	} else {
		success_hook(data, options, api_call, textStatus);
	}
}, _mangle:function (params) {
	p = {};
	p.data = {};
	p.options = {};
	p.callback = null;
	if (params.length < 1 || typeof params[0] != "string") {
		throw "Invalid api_call.";
	}
	p.api_call = params[0];
	fields = ["data", "options", "options"];
	_i = 0;
	for (i = 1; i < 4; i++) {
		if (!params[i]) {
			continue;
		}
		if ($.isFunction(params[i])) {
			p.callback = params[i];
			continue;
		}
		p[fields[_i]] = params[i];
		_i++;
	}
	if (!p.callback && p.options.success && $.isFunction(p.options.success)) {
		p.callback = p.options.success;
	}
	return p;
}, get:function (api_call, data, options, callback) {
	p = this._mangle([api_call, data, options, callback]);
	p.options.type = "GET";
	this.request(p.api_call, p.data, p.options, p.callback);
}, post:function (api_call, data, options, callback) {
	p = this._mangle([api_call, data, options, callback]);
	p.options.type = "POST";
	this.request(p.api_call, p.data, p.options, p.callback);
}, request:function (api_call, data, options, callback) {
	_this = this;
	success_hook = $.isFunction(callback) ? callback : null;
	error_hook = $.isFunction(options.error) ? options.error : null;
	complete_hook = $.isFunction(options.complete) ? options.complete : null;
	if (options.data) {
		$.extend(data, options.data);
	}
	$.each(["complete", "data", "error", "success"], function (i, v) {
		delete options[v];
	});
	jquery_options = {};
	_jqopt = ["async", "beforeSend", "cache", "complete", "contentType", "context", "data", "dataFilter", "dataType", "error", "global", "ifModified", "jsonp", "jsonpCallback", "password", "processData", "scriptCharset", "success", "timeout", "traditional", "type", "url", "username", "xhr"];
	$.each(options, function (key, value) {
		if ($.inArray(key, _jqopt) != -1) {
			jquery_options[key] = value;
			delete options[key];
		}
	});
	success_callback = function (data, textStatus, xhr) {
		if (data.error) {
			_error = parseInt(data.error);
			if (_error) {
				data.error = _error;
			} else {
				data.msg = data.error;
				data.error = 1;
			}
		} else {
			data.error = 0;
			if (!data.msg) {
				data.msg = "";
			}
		}
		if (!data.success || data.error != 0) {
			_this._error(api_call, options, data, textStatus, "BackendError", error_hook);
		} else {
			_this._success(api_call, options, data, textStatus, success_hook);
		}
	};
	error_callback = function (xhr, textStatus, errorThrown) {
		data = {success:false, error:-1, msg:"An error has occurred."};
		_this._error(api_call, options, data, textStatus, errorThrown, error_hook);
	};
	complete_callback = function (xhr, textStatus) {
		_this._complete(api_call, options, textStatus, complete_hook);
	};
	$.extend(data, {call:api_call});
	ajax_options = {cache:false, complete:complete_callback, data:data, dataType:"json", error:error_callback, success:success_callback, type:"POST", url:"/ajax"};
	$.extend(ajax_options, jquery_options);
	_this._begin(api_call, options);
	$.ajax(ajax_options);
}}};
AboutMe.init = function () {
	$("#header ul.nav li.people a.toggle").click(function () {
		$("#header ul.nav li.people ul.people-menu").stop().toggleFade({speedIn:150, speedOut:250});
		return false;
	});
	$("body").click(function () {
		$("#header ul.nav li.people ul.people-menu").stop().fadeOut(250);
	});
};
$(document).ready(AboutMe.init);
var DirectoryCarousel = {setupCarousel:function (which) {
	which.cached = [];
	if (typeof (which.imagePath) != "function") {
		which.imagePath = DirectoryCarousel.getImagePath;
	}
	var exampleclassname = "example";
	if (which.imgsize == "400x270") {
		exampleclassname += " example-large";
	}
	var hh = "";
	for (var i = 0; i < which.people.length; i++) {
		if (i % which.peopleperpage == 0) {
			hh += "<li class=\"page clearfix\">";
			which.cached.push(false);
		}
		var person = which.people[i];
		var displayname = $("<div />").text(person[1]).html();
		hh += "<a href=\"http://about.me/" + person[0] + "\" class=\"" + exampleclassname + "\">";
		hh += "<span class=\"thumb\"><img src=\"" + AboutMe.g.STATIC_HOSTNAME + "/images/directory/loading-" + which.imgsize + ".png\" alt=\"" + displayname + "\" /></span>";
		hh += "<span class=\"name\">" + displayname + "&nbsp;</span>";
		hh += "</a>";
		if (i % which.peopleperpage == which.peopleperpage - 1) {
			hh += "</li>";
		}
	}
	if ((which.people.length - 1) % which.peopleperpage < which.peopleperpage - 1) {
		hh += "</li>";
	}
	$(which.selector).html(hh);
	$(which.selector).jcarousel({scroll:1, animation:500, buttonNextHTML:null, buttonPrevHTML:null, itemFallbackDimension:900, initCallback:function (carousel) {
		carousel.which = which;
		carousel.node = $(which.selector);
		carousel.nav = $(which.navselector);
		carousel.nav.empty();
		if (carousel.options.size > 1) {
			carousel.nav.append("<span class=\"button button-small button-blue button-first\"><span class=\"button-content\">&laquo;</span></span>");
			if (which.linkpagenumbers) {
				for (var i = 1; i <= carousel.options.size; i++) {
					carousel.nav.append("<span class=\"button button-small button-blue\"><span class=\"button-content\">" + i + "</span></span>");
				}
			} else {
				carousel.nav.append("<span class=\"button button-small button-blue button-active\"><span class=\"button-content\">1 of " + carousel.options.size + "</span></span>");
			}
			carousel.nav.append("<span class=\"button button-small button-blue button-last\"><span class=\"button-content\">&raquo;</span></span>");
		}
		DirectoryCarousel.preloadPage(carousel, 0);
		carousel.scroll(1);
		DirectoryCarousel.updateCarouselNav(carousel);
		carousel.nav.find(".button").not(".button-first, .button-last").bind("click", function () {
			var btn = $(this);
			if (btn.hasClass("button-active")) {
				return false;
			}
			var index = $.jcarousel.intval(btn.text());
			DirectoryCarousel.preloadPage(carousel, index);
			carousel.scroll(index);
			DirectoryCarousel.updateCarouselNav(carousel);
			return false;
		});
		carousel.nav.find(".button-last").bind("click", function () {
			carousel.next();
			DirectoryCarousel.updateCarouselNav(carousel);
			return false;
		});
		carousel.nav.find(".button-first").bind("click", function () {
			carousel.prev();
			DirectoryCarousel.updateCarouselNav(carousel);
			return false;
		});
	}});
}, preloadPage:function (carousel, index) {
	if (index < 0) {
		return;
	}
	if (index >= carousel.which.cached.length) {
		return;
	}
	if (carousel.which.cached[index]) {
		return;
	}
	carousel.node.find("li.page").eq(index).find("img").each(function (i) {
		var person = carousel.which.people[(index * carousel.which.peopleperpage) + i];
		if (person) {
			var _this = this;
			this.onerror = function () {
				this.src = AboutMe.g.STATIC_HOSTNAME + "/images/directory/thumbnailnotavailable-" + carousel.which.imgsize + ".png";
			};
			this.src = carousel.which.imagePath(carousel.which.imgsize, person[0]);
		}
	});
	carousel.which.cached[index] = true;
}, getImagePath:function (size, username) {
	return AboutMe.g.STATIC_HOSTNAME + "/images/directory/thumbnails/" + size + "/" + username + ".jpg";
}, updateCarouselNav:function (carousel) {
	var index = carousel.first;
	if (index > carousel.options.size) {
		index = index % carousel.options.size;
	}
	var buttons = carousel.nav.find(".button").not(".button-first, .button-last");
	if (carousel.which.linkpagenumbers) {
		buttons.removeClass("button-active");
		buttons.eq(index - 1).addClass("button-active");
	} else {
		buttons.eq(0).find("span.button-content").html(index + " of " + carousel.options.size);
	}
	var prevbutton = carousel.nav.find(".button-first");
	if (index != 1) {
		prevbutton.removeClass("button-disabled");
	} else {
		prevbutton.addClass("button-disabled");
	}
	var nextbutton = carousel.nav.find(".button-last");
	if (index != carousel.options.size) {
		nextbutton.removeClass("button-disabled");
	} else {
		nextbutton.addClass("button-disabled");
	}
	DirectoryCarousel.preloadPage(carousel, index - 1);
	DirectoryCarousel.preloadPage(carousel, index + 1);
}};
var Bevel = {getDecimalTriplet:function (hexTriplet) {
	var triplet = new Array();
	triplet[0] = parseInt(hexTriplet.slice(0, 2), 16);
	triplet[1] = parseInt(hexTriplet.slice(2, 4), 16);
	triplet[2] = parseInt(hexTriplet.slice(4), 16);
	return triplet;
}, getHexByte:function (decimalByte) {
	var raw = decimalByte.toString(16);
	if (raw.length < 2) {
		raw = "0" + raw;
	}
	return (raw);
}, getHexTriplet:function (decimalTriplet) {
	var triplet = Bevel.getHexByte(decimalTriplet[0]) + Bevel.getHexByte(decimalTriplet[1]) + Bevel.getHexByte(decimalTriplet[2]);
	return (triplet);
}, shiftAxis:function (value) {
	var upper = 50;
	var lower = 20;
	var darkness = 255 - value;
	var d_extreme = Math.min(darkness, value);
	var d_middle = 127 - d_extreme;
	var variation = Math.floor((upper - lower) * (d_middle / 127));
	var contrast = lower + variation;
	var lightness_parameter = (Math.pow(value, 4) / Math.pow(255, 4)) + 1;
	var up = Math.min(255, Math.floor(value + (contrast)));
	var down = Math.max(0, Math.floor(value - (lightness_parameter * contrast)));
	return ([up, down]);
}, getBevelColors:function (color) {
	var triplet = Bevel.getDecimalTriplet(color);
	var beveling = new Array();
	beveling[0] = new Array();
	beveling[1] = new Array();
	for (key in triplet) {
		var axis = Bevel.shiftAxis(triplet[key]);
		beveling[0].push(axis[0]);
		beveling[1].push(axis[1]);
	}
	var beveled = new Array();
	beveled[0] = "#" + Bevel.getHexTriplet(beveling[0]);
	beveled[1] = "#" + Bevel.getHexTriplet(beveling[1]);
	return (beveled);
}, updateBevel:function (color) {
	var beveled = Bevel.getBevelColors(color);
	var top = "1px solid " + beveled[1];
	var bottom = "1px solid " + beveled[0];
	$("#profile-bevel").css({"height":"0px", "background":"transparent", "border-top":top, "border-bottom":bottom});
}};
function debug(mm) {
	if (window.console) {
		if (console.log) {
			console.log(mm);
		}
	}
}
handleReturnKey = function (e, callback) {
	if (e && callback && $.isFunction(callback)) {
		var key = (window.Event) ? e.which : e.keyCode;
		if (key && key == 13) {
			callback();
		}
	}
};
handleEscapeKey = function (e, callback) {
	if (e && callback && $.isFunction(callback)) {
		var key = (window.Event) ? e.which : e.keyCode;
		if (key && key == 27) {
			callback();
		}
	}
};
function getQuery(key, loc) {
	var query = "";
	key = key || "?";
	loc = loc || document.location.href;
	if (loc.indexOf(key) > -1) {
		query = loc.substring(loc.indexOf(key) + key.length, loc.length);
	}
	return query;
}
function parseQuery(key, loc) {
	var qq = new Array();
	var query = getQuery(key, loc);
	if (query) {
		var args = query.split("&");
		for (i = 0; i < args.length; i++) {
			var chunk = args[i];
			qq[chunk.split("=")[0]] = unescape(chunk.split("=")[1]);
		}
	}
	return qq;
}

